package com.shopping.dao;

import com.shopping.pojo.Order;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Mapper
public interface OrderDao {

    String TABLE_NAME = "orders";
    String INSERT_FIELDS = "user_id, trade_id, number, time, receive_id, transport_num, store_id";
    String SELECT_FIELDS = "id, user_id userId, trade_id tradeId, appraise_id appraiseId, number, time, " +
            "receive_id receiveId, transport_num transportNum, store_id storeId";

    @Insert({"insert into ", TABLE_NAME, " (", INSERT_FIELDS, ") values(#{order.userId}, #{order.tradeId}, #{order.number}, #{order.time}, #{order.receiveId}, #{order.transportNum}, #{order.storeId})"})
    int insert(@Param("order") Order order);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where id=#{id}"})
    Order showOneOrder(@Param("id") int id);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where user_id=#{userId}"})
    List<Order> showUserOrders(@Param("userId") int userId);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where store_id=#{storeId}"})
    List<Order> showStoreOrders(@Param("storeId") int storeId);

    @Update({"update ", TABLE_NAME, " set appraise_id=#{appraiseId} where store_id=#{storeId} and trade_id=#{tradeId} and user_id=#{userId}"})
    int updateAppraise(@Param("appraiseId")int appraiseId, @Param("storeId") int storeId, @Param("tradeId") int tradeId, @Param("userId") int userId);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where store_id=#{storeId} and trade_id=#{tradeId} and user_id=#{userId}"})
    Order selectOrderExist(@Param("userId") int userId, @Param("storeId") int storeId, @Param("tradeId") int tradeId);

    @Select({"select last_insert_id()"})
    Integer lastInsertId();

    @Select({"select user_id from ", TABLE_NAME, " where id=#{orderId}"})
    int selectUserIdByOrderId(@Param("orderId") int orderId);

}
